﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using HMS.Data;
using HMS.ClassLibrary;
using System.Threading;
using System.Windows.Interop;

namespace HMS
{
    /// <summary>
    /// Interaction logic for LoginWindow.xaml
    /// </summary>
   
    public partial class LoginWindow : Window
    {
        private HMSEntities HMSContext;
        private ProgressWindow ProgressWindow;

        public LoginWindow()
        {
            InitializeComponent();
            HMSContext = new HMSEntities();
            InitializeControlState();
            this.Loaded += new RoutedEventHandler(Window_Loaded);
        }
        private void InitializeControlState()
        {
            UserNameTextBox.Text = "";
            FocusManager.SetFocusedElement(this,UserNameTextBox);
            PasswordTextBox.Password = "";
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
               //Enable Glass effect 
               GlassEffect GlassEffectObj=new GlassEffect(new WindowInteropHelper(this).Handle);
               GlassEffectObj.EnableGlassEffect();

            }
            catch (Exception ex)
            {
                
                 MessageBox.Show(ex.Message,Properties.Resources.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Exclamation);
        
            }
        }
        private bool ValidateControlValues()
        {
            if (String.IsNullOrEmpty(UserNameTextBox.Text.Trim()))
            {
                MessageBox.Show(Properties.Resources.UserNameNotSet, Properties.Resources.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Information);
                return false;
            }
            if (String.IsNullOrEmpty(PasswordTextBox.Password.Trim()))
            {
                MessageBox.Show(Properties.Resources.PasswordNotSet, Properties.Resources.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Information);
                return false;
            }  
            return true;
        }
        private void Login()
        {
            try
            {
                if (ValidateControlValues())
                {
                    string userName=UserNameTextBox.Text.Trim();
                    string password=PasswordTextBox.Password.Trim();

                    Cryptography cryptograph=new Cryptography();
                    string encryptedPassword=cryptograph.Encrypt(password);

                    int rowCount= HMSContext.Users.Where(u => u.UserName == userName && u.Password == encryptedPassword).Count();
                    if (rowCount > 0)
                    {
                        User user = HMSContext.Users.Where(u => u.UserName == userName && u.Password == encryptedPassword).First();
                        HMSApplication.Session = new Session(user);
                        MainWindow mainWindow = new MainWindow();
                        mainWindow.Show();
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show(Properties.Resources.InvalidLogin, Properties.Resources.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Error);
                 
                    }

                }
            }
            catch (Exception)
            {
                
                throw;
            }    

        }
        private void LoginBackgroundWorker(object sender, DoWorkEventArgs e)
        {
            try
            {
                //dispathcher needs to access ui control without having exceptions
                this.Dispatcher.Invoke((Action)(() =>
                {
                    this.Login();
                }));

            }
            catch (Exception)
            {
                throw;
            }
        }
        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            App.Current.Shutdown();
        }

        private void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //show the busy indicator of saving
                if (ValidateControlValues())
                {
                    ProgressWindow = new ProgressWindow(this.LoginBackgroundWorker);
                    ProgressWindow.Owner = this;
                    ProgressWindow.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Properties.Resources.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }     
        }

      
    }
}
